<?php
/************************************************************************************************
 * Copyright (c) 2004-2009, codesign
 * 
 * All rights reserved.
 * 
 * LICENCE CC/BSD
 * You are free:
 * 
 * to Share & to copy, distribute and transmit the work
 * to Remix & to adapt the work
 * 
 * Under the following conditions:
 * 
 * Attribution. You must attribute the work in the manner specified by the author or 
 * licensor (but not in any way that suggests that they endorse you or your use of the work).
 * Share Alike. If you alter, transform, or build upon this work, you may distribute 
 * the resulting work only under the same, similar or a compatible license.
 * For any reuse or distribution, you must make clear to others the license terms of 
 * this work. The best way to do this is with a link to this web page.
 * Any of the above conditions can be waived if you get permission from the copyright holder.
 * Nothing in this license impairs or restricts the author's moral rights.
 * 
 * LICENCE BSD
 * Redistribution and use in source and binary forms, with or without modification, 
 * are permitted provided that the following conditions are met:
 * 
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright notice, 
 * this list of conditions and the following disclaimer in the documentation 
 * and/or other materials provided with the distribution.
 * Neither the name of "codesign" nor the names of its contributors may be used 
 * to endorse or promote products derived from this software without specific prior written permission.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 ************************************************************************************************/
include_once '_prerun';

define( 'SITE_ROOT', str_replace('//', '/', FILE_ROOT . 'site/') );
define( 'PAGE_ROOT', SITE_ROOT );
define( 'TPL_ROOT', 	SITE_ROOT . 'site/fields/' );

CRON_DAY( FILE_ROOT . '__cronday' );

////////////////////////////////////////////////////////////////////////////////
function nobr( $value, $replace = '' )
	{
	$value = str_replace( "\r\n", $replace, $value );
	$value = str_replace( "\n\r", $replace, $value );
	$value = str_replace( "\r", $replace, $value );
	$value = str_replace( "\n", $replace, $value );
	return $value;
	}

////////////////////////////////////////////////////////////////////////////////
function valueFromLang( $value, $lang )
	{
	if( strpos( $value, '<span lang="'.$lang.'">' ) )
		{
		if( preg_match( '/\<span lang="'.$lang.'"\>(.*)\<\/span\>/', $value, $matches ) )
			{
			$value = $matches[ 1 ];
			}
		}
	else
	if( strpos( $value, '|' ) )
		{
		$values = explode( '|', $value );
		if( $lang == 'fr' )
			$value = $values[0];
		else
			$value = $values[1];
		}
	return $value;
	}

////////////////////////////////////////////////////////////////////////////////
function tag_s( $tag_name )
	{
	echo "<{$tag_name}>\n";
	}

////////////////////////////////////////////////////////////////////////////////
function tag_e( $tag_name )
	{
	echo "</{$tag_name}>\n";
	}

////////////////////////////////////////////////////////////////////////////////
function text( $tag_name, $value, $max_char = 0 )
	{
	$value = preg_replace( '|<strong>([^</strong>]+)</strong>|is', '<b>\\1</b>', $value );
	$value = preg_replace( '|<em>([^</em>]+)</em>|is', '<i>\\1</i>', $value );
	$value = strip_tags( utf8_encode( $value ), 'b,i' );
	if( $max_char > 0 )
		{
		USE_LIB( 'template' );
		$value = str_replace( "\r", '', $value );
		$value = str_replace( "\n", '', $value );
		$value = Template::limitText( $value, $max_char );
		}
	echo "<{$tag_name}><![CDATA[{$value}]]></{$tag_name}>\n";
	}

////////////////////////////////////////////////////////////////////////////////
function num( $tag_name, $value )
	{
	echo "<{$tag_name}>{$value}</{$tag_name}>\n";
	}

////////////////////////////////////////////////////////////////////////////////
function tag_array( $enclosing_tag, $values, $lang )
	{
	tag_s( $enclosing_tag );
	foreach( $values as $id => $value )
		{
		tag_s( 'item' );
		num( 'id', $id );
		text( 'value', valueFromLang( $value, $lang ) );
		tag_e( 'item' );
		}
	tag_e( $enclosing_tag );
	}


////////////////////////////////////////////////////////////////////////////////
ob_start();
echo "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n";
tag_s( 'root' );

try
	{
	$datas = $_REQUEST;
	
	if( isset( $datas[ 'function' ] ) && file_exists( PAGE_ROOT . 'flash_' . $datas[ 'function' ] . '.php5' ) )
		{
		$file = PAGE_ROOT . 'flash_' . $datas[ 'function' ] . '.php5';
		$lang = $datas[ 'lang' ];
		text( 'function', $datas[ 'function' ] );
		text( 'lang', $datas[ 'lang' ] );
		unset( $datas[ 'function' ] );
		unset( $datas[ 'lang' ] );
		tag_s( 'datas' );
			include_once( $file );
		tag_e( 'datas' );
		num( 'retour', 1 );
		text( 'message', '' );
		if( isset( $_REQUEST['redirect'] ) && ( $_REQUEST['redirect'] && $_REQUEST['websvc'] ) )
			{
			header('Location:' . $_REQUEST['redirect']);
			die();
			}
		}
	else
		{
		throw new Exception( 'Impossible d\'interpréter cette commande...' );
		}
	}
catch( Exception $e )
	{
	num( 'retour', 0 );
	text( 'message', $e->getMessage() );
	}

tag_e( 'root' );


header( 'Content-type: text/xml; charset=utf-8' );
ob_end_flush();
?>